//
//  PrepayCalculator.m
//  Rong360
//
//  Created by tianliwei on 16/2/14.
//  Copyright (c) 2014 tianliwei. All rights reserved.
//

#import "PrepayCalculator.h"

@implementation PrepayCalculator

- (void)beginCalculateWithPrepayType:(PrepayType)type{
    CGFloat dkze = 0;//贷款总额
    CGFloat dkll = 0;//月利率
    NSInteger dkqs = 0;//贷款期数
    NSInteger hkqs = 0;//还款期数
    NSDate *firstDate = nil;
    if (type == PrePayPart) {
        dkze = [self.dkze_part floatValue] * 10000.0;
        dkll = [self.dkll_part floatValue] / 1200.0;
        dkqs = ([self.deadline_part floatValue] * 12);
        NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;
        NSDateComponents *components = [gregorian components:unitFlags fromDate:self.firstPay_part toDate:self.prePay_part options:0];
        hkqs = [components month];
        firstDate = self.firstPay_part;
    }else if (type == PrePayAll){
        dkze = [self.dkze_all floatValue] * 10000.0;
        dkll = [self.dkll_all floatValue] / 1200.0;
        dkqs = ([self.deadline_all floatValue] * 12);
        NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit;
        NSDateComponents *components = [gregorian components:unitFlags fromDate:self.firstPay_all toDate:self.prePay_all options:0];
        hkqs = [components month];
        firstDate = self.firstPay_all;
    }
    
    self.plan_Date = [DateUtil addTimeFromDate:firstDate addMonths:dkqs];//
    
    CGFloat yhk = dkze * dkll * pow((1+dkll), dkqs) / (pow((1+dkll), dkqs) - 1);//每月还款
    CGFloat yhje = yhk * hkqs;//已还金额
    
    CGFloat yhlx = 0;//已还利息
    CGFloat yhbj = 0;//已还本金
    
    CGFloat byhk = 0;//本月还款
    NSInteger xdkqs = 0;
    CGFloat xyhk = 0;//下月还款
    CGFloat jslx = 0;//节省利息
    for (int i = 1; i <= hkqs; i++) {
        yhlx = yhlx + (dkze - yhbj) * dkll;
        yhbj = yhbj + yhk - (dkze - yhbj) * dkll;
    }
    if (type == PrePayPart) {
        CGFloat tqhq = [self.tqhq_part floatValue] * 10000.0;//提前还清
        if (tqhq + yhk > (dkze - yhbj) * (1 + dkll)) {
            NSLog(@"您的提前还款额已足够还清所欠贷款！");
        }else{
            yhbj = yhbj + yhk;
            byhk = yhk + tqhq;//该月一次还款额
            if (self.hkType == YueGongAlways) {
                CGFloat yhbjs_temp = yhbj +tqhq;
                for (xdkqs = 0; yhbjs_temp < dkze; xdkqs++)//before is '<=' changed by tian 14/03/24
                    yhbjs_temp = yhbjs_temp + yhk - (dkze - yhbjs_temp) * dkll;
                xdkqs = xdkqs - 1;
                xyhk = (dkze - yhbj - tqhq) * (dkll * pow((1 + dkll), xdkqs)) / (pow((1 + dkll), xdkqs) - 1);
                jslx = yhk * dkqs - hkqs - byhk - xyhk * xdkqs;
                self.true_Date =  [DateUtil addTimeFromDate:firstDate addMonths:xdkqs];
            }else if (self.hkType == QiXianAlways){
                xyhk = (dkze - yhbj - tqhq) * (dkll * pow((1 + dkll), (dkqs - hkqs))) / (pow((1 + dkll), (dkqs - hkqs)) - 1);
                jslx = yhk * dkqs - hkqs - byhk - xyhk * (dkqs - hkqs);
                self.true_Date = self.plan_Date;
            }
        }
    }else if (type == PrePayAll){
        byhk = (dkze - yhbj) * (1 + dkll);
        xyhk = 0;
        jslx = yhk * dkqs - yhje - byhk;
        self.true_Date = [DateUtil stringWithDate:self.prePay_all];
    }
    self.crate_saved = [NSString stringWithFormat:@"%.1f",jslx];
    self.cmonth = [NSString stringWithFormat:@"%.1f",byhk];//changed by tian 14/3/25
    self.cmonth_new = [NSString stringWithFormat:@"%.1f",xyhk];
    self.cmonth_old = [NSString stringWithFormat:@"%.1f",yhk];

    self.totalPayed = [NSString stringWithFormat:@"%.1f",yhje];
    self.ratePayed = [NSString stringWithFormat:@"%.1f",yhlx];
    self.results = @[self.crate_saved,self.cmonth,self.cmonth_new,self.cmonth_old,self.true_Date,self.plan_Date,self.totalPayed,self.ratePayed];
}

@end
